package com.hue.gulimail.gulimailorder.config;

import com.zaxxer.hikari.HikariDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;

/**
 * @Author: 姚凤桥
 * @Date: 2021/12/29 21:36
 * @Description: seata配置
 */
@Configuration
public class SeataConfig {
    /**
     * 使用DataSourceProxy来代理DataSource
     * @param properties: 数据库配置文件绑定类
     * @return
     */
    @Bean
    public DataSource dataSource(DataSourceProperties properties) {
        //手动创建数据源
        HikariDataSource hikariDataSource =
                properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
        //设置数据源名字
        if (StringUtils.hasText(properties.getName())) {
            hikariDataSource.setPoolName(properties.getName());
        }

        return new DataSourceProxy(hikariDataSource);
    }
}
